package cn.yuxiaoyuan.service.impl;

import cn.hutool.crypto.SecureUtil;
import cn.yuxiaoyuan.common.util.R;
import cn.yuxiaoyuan.common.util.RandomStringUtils;
import cn.yuxiaoyuan.mapper.UserMapper;
import cn.yuxiaoyuan.service.IUserService;
import cn.yuxiaoyuan.service.base.impl.BaseServiceImpl;
import cn.yuxiaoyuan.user.domain.User;
import cn.yuxiaoyuan.user.query.UserQuery;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author solargen
 * @version 1.0
 * @date 2025/5/11 10:36
 */
@Service
public class UserServiceImpl extends BaseServiceImpl<User> implements IUserService {
    @Autowired
    private UserMapper userMapper;
    @Override
    public void add(User user) {
        //得到用户输入的密码
        String password = user.getPassword();
        //设置盐值 随机值
        String s = RandomStringUtils.randomAlphabetic(10);
        user.setSalt(s);
        String s1 = SecureUtil.md5(password + s);
        user.setPassword(s1);
        userMapper.add(user);
    }
    @Override
    public R pageList(UserQuery user) {
        PageHelper.startPage(user.getPage(),user.getSize());//自动在后面的哪个sql里面加上limit
        List<User> users = userMapper.pageList(user);//在执行这个sql之前
        return  R.ok(new PageInfo<>(users));
    }
}
